package docsync;

import java.util.HashMap;

public class ExtensionMapper {
	HashMap<String, String> extmap = new HashMap<String, String>();
	HashMap<String, String> typemap = new HashMap<String, String>();

	public ExtensionMapper() {

		extmap.put("csv", "text/csv");
		extmap.put("tsv", "text/tab-separated_values");
		extmap.put("tab", "text/tab-separated_values");
		extmap.put("html", "text/html");
		extmap.put("htm", "text/html");
		extmap.put("doc", "application/msword");
		extmap.put("docx", "application" + "/vnd.openxmlformats-officedocument"
				+ ".wordprocessingml.document");
		extmap.put("ods", "application"
				+ "/x-vnd.oasis.opendocument.spreadsheet");
		extmap.put("odt", "application" + "/vnd.oasis.opendocument.text");
		extmap.put("rtf", "application/rtf");
		extmap.put("sxw", "application/vnd.sun.xml.writer");
		extmap.put("txt", "text/plain");
		extmap.put("xls", "application/vnd.ms-excel");
		extmap.put("xlsx", "application/vnd.openxmlformats-officedocument"
				+ ".spreadsheetml.sheet");
		extmap.put("pdf", "application/pdf");
		extmap.put("ppt", "application/vnd.ms-powerpoint");
		extmap.put("pps", "application/vnd.ms-powerpoint");
		extmap.put("wmf", "image/x-wmf");

		// default type map
		typemap.put("document", "doc");
		typemap.put("spreadsheet", "xls");
	}

	public String fromExtension(String ext) {
		return extmap.get(ext.toLowerCase());
	}

	public String fromFilename(String name) {
		String ext = name.substring(1 + name.lastIndexOf('.'));
		return fromExtension(ext);
	}

	public String fromType(String typename) {
		return fromExtension(typemap.get(typename.toLowerCase()));
	}

	public String fromResourceId(String resourceId) {
		return fromType(resourceId.substring(0, resourceId.indexOf(':')));
	}

	public String resourceIdToExtension(String resourceId) {
		String type = resourceId.substring(0, resourceId.indexOf(':'));
		return typemap.get(type.toLowerCase());
	}

}